home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 3 / L'Effet Pommier - Volume 03.iso / Programmation / gray image 2.1 / morph_filter.h < prev    next >
Text File  |  1995-05-31  |  4KB  |  130 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /*
  3.  ************************************************************************
  4.  *
  5.  *               Grayscale Image
  6.  *
  7.  *           Morphological filtration of the image
  8.  *    
  9.  * Algorithm
  10.  *    Described in the paper
  11.  *    P.Maragos, Pattern Spectrum and Multiscale Shape Representation,
  12.  *    IEEE Trans. Pattern Anal. Machine Intell., vol. 11, N7,
  13.  *    p. 701-716, July 1989.
  14.  *
  15.  * The program implements the Function-Set-Processing (FSP) for the 
  16.  * gray-scale images and boolean pattern. Refer to the Sec. II D of the
  17.  * paper.
  18.  *
  19.  * Specifically, a dilation operation is defined as 
  20.  *    dest = src PLUS pattern, i.e.
  21.  *     dest(k,l) = MAX{ src(k-i,l-j) } i=pattern.x(n), j=pattern.y(n)
  22.  *    n=0..pattern.card,
  23.  * dest and src being the images before and after the operation, resp.
  24.  * On the other hand, erosion is
  25.  *    dest = src MINUS pattern, i.e.
  26.  *    dest(k,l) = MIN{ src(k+i,l+j) } i=pattern.x(n), j=pattern.y(n)
  27.  *    n=0..pattern.card
  28.  * Out-of-image pixels for src are assumed to be zeros
  29.  *
  30.  * Opening is the dilation followed by the erosion, and closing,
  31.  * in contrast, is the erosion followed by the dilation.
  32.  * It is easy to see that both erosion and dilation introduce some
  33.  * phase shift in the image. Moreover, the shifts have the same size 
  34.  * but different sign. That's why they get cancelled out in opening and
  35.  * closing.
  36.  *
  37.  * $Id$
  38.  *
  39.  ************************************************************************
  40.  */
  41.  
  42. #ifdef __GNUC__
  43. #pragma interface
  44. #endif
  45.  
  46. #include "image.h"
  47.  
  48. /*
  49.  *------------------------------------------------------------------------
  50.  *        Defining a class for the Binary Pattern
  51.  * Convention:
  52.  *    The entire pattern has to be located within the I quadrant, i.e.
  53.  *    x(n) >= 0, y(n) >= 0 for each of the pattern points
  54.  */
  55.  
  56.                      // Names for some standard patterns
  57. enum PatternNames {Square, Octagon};
  58.  
  59. class BinaryPattern {
  60.  
  61.   int npoints;            // No. of points in the pattern
  62.   int * xs;            // Pattern point abscissae
  63.   int * ys;            // Pattern point ordinates
  64.  
  65.   void allocate(const int npoints);    // Allocate the pattern structure
  66.  
  67. public:
  68.   BinaryPattern(const int npoints);    // Create an empty pattern
  69.                     // Create a pattern of a predefined
  70.                     // shape
  71.   BinaryPattern(const PatternNames name,const int size);
  72.   ~BinaryPattern();
  73.  
  74.                     // Q. cardinality of the pattern
  75.   int card(void) const            { return npoints; }
  76.   int x(const int pointn) const;    // Get the coordinate of a
  77.   int y(const int pointn) const;    // specific pattern point
  78.  
  79.                 // Pattern morphological operations
  80.   friend void dilation(IMAGE& im, const BinaryPattern& pattern);
  81.   friend void erosion(IMAGE& im, const BinaryPattern& pattern);
  82.  
  83.   friend inline void opening(IMAGE& im, const BinaryPattern& pattern);
  84.   friend inline void closing(IMAGE& im, const BinaryPattern& pattern);
  85. };
  86.  
  87. /*
  88.  *------------------------------------------------------------------------
  89.  *               Inline Procedures
  90.  */
  91.  
  92.                 // Create an empty pattern
  93. inline BinaryPattern::BinaryPattern(const int npoints)
  94. {
  95.   allocate(npoints);
  96. }
  97.  
  98.                 // Get the x coordinate of a specific
  99.                 // pattern point
  100. inline int BinaryPattern::x(const int pointn) const
  101. {
  102.   if( pointn < 0 || pointn >= npoints )
  103.     _error("The no. for the point requested %d is out of range [0,%d)",
  104.        pointn, npoints);
  105.   return xs[pointn];
  106. }
  107.  
  108.                 // Get the y coordinate of the specific
  109.                 // pattern point
  110. inline int BinaryPattern::y(const int pointn) const
  111. {
  112.   if( pointn < 0 || pointn >= npoints )
  113.     _error("The no. for the point requested %d is out of range [0,%d)",
  114.        pointn, npoints);
  115.   return ys[pointn];
  116. }
  117.  
  118.  
  119. inline void opening(IMAGE& im, const BinaryPattern& pattern)
  120. {
  121.   dilation(im,pattern);
  122.   erosion(im,pattern);
  123. }
  124.  
  125. inline void closing(IMAGE& im, const BinaryPattern& pattern)
  126. {
  127.   erosion(im,pattern);
  128.   dilation(im,pattern);
  129. }
  130.